package com.fc.test.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.fc.test.mapper.auto.ForeClassMapper;
import com.fc.test.model.auto.ForeClass;
import com.fc.test.model.auto.ForeClassExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.Convert;
import com.fc.test.mapper.auto.ForeClassUserMapper;
import com.fc.test.model.auto.ForeClassUser;
import com.fc.test.model.auto.ForeClassUserExample;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;

/**
 *  ForeClassUserService
 * @Title: ForeClassUserService.java 
 * @Package com.fc.test.service 
 * @author fuce_自动生成
 * @email 115889198@qq.com
 * @date 2020-04-19 11:39:50  
 **/
@Service
public class ForeClassUserService implements BaseService<ForeClassUser, ForeClassUserExample>{
	@Autowired
	private ForeClassUserMapper foreClassUserMapper;
	
      	   	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Autowired
	ForeClassMapper foreClassMapper;
	 public PageInfo<ForeClassUser> list(Tablepar tablepar,String name,String teacherId){
	        ForeClassUserExample testExample=new ForeClassUserExample();
	        testExample.setOrderByClause("id ASC");
		 if(name!=null&&!"".equals(name)){
			 ForeClassExample foreClassExample=new ForeClassExample();
			 foreClassExample.or().andNameEqualTo(name);
			 List<ForeClass> list1=foreClassMapper.selectByExample(foreClassExample);
			 if(list1.size()!=0)name=list1.get(0).getId();
		 }
		 ForeClassUserExample.Criteria criteria=testExample.createCriteria();
	        if(name!=null&&!"".equals(name)){
	        	criteria.andClassIdEqualTo(name);
	        }

		 ForeClassExample example=new ForeClassExample();
		 example.or().andTeacherIdEqualTo(teacherId);
		 List<ForeClass> foreClasses=foreClassMapper.selectByExample(example);
		 List<String> classIds=new ArrayList();
		 for(ForeClass foreClass:foreClasses){
			 classIds.add(foreClass.getId());
		 }
		 criteria.andClassIdIn(classIds);
	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<ForeClassUser> list= foreClassUserMapper.selectByExample(testExample);
	        PageInfo<ForeClassUser> pageInfo = new PageInfo<ForeClassUser>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {
				
			List<String> lista=Convert.toListStrArray(ids);
			ForeClassUserExample example=new ForeClassUserExample();
			example.createCriteria().andIdIn(lista);
			return foreClassUserMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public ForeClassUser selectByPrimaryKey(String id) {
				
			return foreClassUserMapper.selectByPrimaryKey(id);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(ForeClassUser record) {
		return foreClassUserMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(ForeClassUser record) {
				
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
			
				
		return foreClassUserMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(ForeClassUser record, ForeClassUserExample example) {
		
		return foreClassUserMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(ForeClassUser record, ForeClassUserExample example) {
		
		return foreClassUserMapper.updateByExample(record, example);
	}

	@Override
	public List<ForeClassUser> selectByExample(ForeClassUserExample example) {
		
		return foreClassUserMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(ForeClassUserExample example) {
		
		return foreClassUserMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(ForeClassUserExample example) {
		
		return foreClassUserMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param foreClassUser
	 * @return
	 */
	public int checkNameUnique(ForeClassUser foreClassUser){
		ForeClassUserExample example=new ForeClassUserExample();
		example.createCriteria().andUserIdEqualTo(foreClassUser.getUserId());
		List<ForeClassUser> list=foreClassUserMapper.selectByExample(example);
		return list.size();
	}


}
